<?php
include_once 'inc/config.php';
include_once 'inc/mysql.class.php';
include_once 'smrty.php';
include_once 'fun/myfunction.php';
$mysql = new mysql();
$mysql->open($dbconfig);
if ($_GET['do']=='newadd') {
	//读取办事处数据
	$banshichu = $mysql->get_one("`id`,`listdeta`,`order_id`","banshichu","`id`=$_GET[id]");
	//读取订单数据
	$order = $mysql->get_one("*","order","`id`=$banshichu[order_id]");
	//得到最大装箱数
	$maxboxmunber = maxboxmunber($order["zhuangxiangshu"]);
	//初始化装箱单数组
	//初始数据数组
	$boxlistarr = array();
	//初始化装箱号变量
	$boxmunber = 1;
	$box = json_decode($banshichu['listdeta'],true);
	$box = $box["banshichu"];

	for ($i = 0; $i < count($box); $i++) {
		foreach ($box[$i] as $ma => $val) {
			if ($ma === "yanse") {
				$yanse = $val;
			}else {
				$maxboxmunber_c = $maxboxmunber[$ma];
				$chushu =floor($val/$maxboxmunber_c);
				if ($chushu > 1) {
					$boxarr[] =array("munber"=>$boxmunber."-".($boxmunber+$chushu-1),
										"yanse"=>$yanse,
										"$ma"=>$maxboxmunber_c);
					$boxmunber += $chushu;
					$box[$i][$ma] -= $maxboxmunber_c*$chushu;

				}elseif ($chushu == 1) {
					$boxarr[] =array("munber"=>$boxmunber,"yanse"=>$yanse,"$ma"=>$maxboxmunber_c);
					$boxmunber++;
					$box[$i][$ma] -= $maxboxmunber_c;
				}
			}
		}
	}
	$array_c = array();
	$chashu_c = 0;
	for ($i = 0; $i < count($box); $i++) {
		foreach ($box[$i] as $ma => $val) {
			if ($ma === "yanse") {
				$arr = array();
				$arr["yanse"]=$val;
				$array_c=array();//新颜色清空中转数组
			}else {
				if (array_sum($array_c)+$val >= reset($maxboxmunber)) {
					foreach ($array_c as $k => $v) {//先将中转数组里的取出
						$arr[$k]=$v;
						$box[$i][$k]-=$v;
					}
					$chashu_c = reset($maxboxmunber)-array_sum($array_c);//处理还差多少够一箱
					$arr[$ma]=$chashu_c;
					$box[$i][$ma] -= $chashu_c;
					$arr["munber"]=$boxmunber;
					$boxarr[]=$arr;//将数组插入到主表里
					$array_c=array();
					$array_c[$ma]=$val-$chashu_c;//清空后将刚才减剩的压入中转数组
					$boxmunber++;
					break;
				}else {
					if ($val>0) {
						$array_c[$ma]=$val;//保存到一个中转数组里
					}
				}
			}
		}
	}
	//处理尾数
	$sum_c = 0;
	for ($i = 0; $i < count($box); $i++) {
		foreach ($box[$i] as $ma => $val) {
			if ($ma === "yanse") {
				$arr = array();
				$arr["yanse"]=$val;
			}else {
				if ($val > 0) {
					if ($sum_c+$val >= reset($maxboxmunber)) {
						$chashu_c = reset($maxboxmunber)-$sum_c;//处理还差多少够一箱
						$arr[$ma]=$chashu_c;
						$arr["munber"]=$boxmunber;
						$boxarr[]=$arr;//将数组插入到主表里
						$arr = array();
						$arr["yanse"]=$box[$i]["yanse"];
						$arr[$ma]=$val-$chashu_c;
						$boxmunber++;
					}else {
						$arr[$ma]=$val;//保存到一个中转数组里
						$sum_c+=$val;
					}
				}
			}
		}
		$arr["munber"]=$boxmunber;
		$boxarr[]=$arr;//将数组插入到主表里
		$arr = array();
	}
	
	//输出表格
	echo "<table width=\"98%\" border=\"1\">";
	foreach ($boxarr as $value) {
		$munbers=munbertoint($value['munber']);
		$sum=$value['s']+$value['m']+$value['l']+$value['xl']+$value['xxl'];
		echo "<tr><td>$value[munber]</td>
					<td>$value[yanse]</td>
					<td>$value[s]</td>
					<td>$value[m]</td>
					<td>$value[l]</td>
					<td>$value[xl]</td>
					<td>$value[xxl]</td>
					<td>$munbers</td>
					<td>$sum</td></tr>";
	}
	echo "</table>";
}
$data = array(
		"boxsum"=>$boxmunber,
		"boxlist"=>addslashes(json_encode($boxarr))//转换反斜杠
);
if ($mysql->update($data, "banshichu","`id`=$banshichu[id]")) {
	echo "生成成功！";
}else
{
	echo "生成失败！";
}
?>